<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
-->

<app-help-message-show
  [help_message_content]="'labels.help' | i18n"
  [guild_link]="'labels.help.link' | i18n"
  [module_name]="'menu.advanced.labels'"
  [icon_name]="'tags'"
></app-help-message-show>

<nz-divider></nz-divider>

<app-toolbar>
  <ng-template #left>
    <button nz-button nzType="primary" nz-tooltip (click)="sync()" [nzTooltipTitle]="'common.refresh' | i18n">
      <i nz-icon nzType="sync" nzTheme="outline"></i>
    </button>
    <button nz-button nzType="primary" (click)="onNewTag()">
      <i nz-icon nzType="appstore-add" nzTheme="outline"></i>
      {{ 'common.button.new' | i18n }}
    </button>
  </ng-template>
  <ng-template #right>
    <app-multi-func-input
      class="mobile-hide"
      groupStyle="width: 250px;"
      [placeholder]="'label.search' | i18n"
      [(value)]="search"
      (keydown.enter)="loadTagsTable()"
      (cleared)="loadTagsTable()"
    />
    <button nz-button nzType="primary" (click)="loadTagsTable()" class="mobile-hide">
      {{ 'common.search' | i18n }}
    </button>
  </ng-template>
</app-toolbar>

<div class="tag-cards-container">
  <nz-row [nzGutter]="[8, 8]">
    <nz-col [nzXs]="12" [nzSm]="8" [nzMd]="6" [nzLg]="4" *ngFor="let data of tags">
      <nz-card [nzActions]="[copyAction, editAction, deleteAction]" [nzBorderless]="true" class="tag-card" [nzSize]="'small'">
        <div class="tag-content">
          <div class="tag-header">
            <a routerLink="/monitors" [queryParams]="{ labels: formatTagName(data) }">
              <nz-tag [nzColor]="data.color" class="tag-name">
                {{ formatTagName(data) }}
              </nz-tag>
            </a>
          </div>

          <div class="tag-description" *ngIf="data.description">
            {{ data.description }}
          </div>
        </div>

        <ng-template #copyAction>
          <span nz-tooltip [nzTooltipTitle]="'common.copy.button' | i18n" (click)="copyTagValue(data)">
            <i nz-icon nzType="copy" nzTheme="outline"></i>
          </span>
        </ng-template>

        <ng-template #editAction>
          <span nz-tooltip [nzTooltipTitle]="'common.button.edit' | i18n" (click)="onEditOneTag(data)">
            <i nz-icon nzType="edit" nzTheme="outline"></i>
          </span>
        </ng-template>

        <ng-template #deleteAction>
          <span nz-tooltip [nzTooltipTitle]="'common.button.delete' | i18n" (click)="onDeleteOneTag(data.id)">
            <i nz-icon nzType="delete" nzTheme="outline"></i>
          </span>
        </ng-template>
      </nz-card>
    </nz-col>
  </nz-row>
</div>

<nz-modal
  [(nzVisible)]="isManageModalVisible"
  [nzTitle]="isManageModalAdd ? ('label.new' | i18n) : ('label.edit' | i18n)"
  (nzOnCancel)="onManageModalCancel()"
  (nzOnOk)="onManageModalOk()"
  nzMaskClosable="false"
  nzWidth="30%"
  [nzOkLoading]="isManageModalOkLoading"
>
  <div *nzModalContent class="-inner-content">
    <form nz-form #tagForm="ngForm">
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="name" nzRequired="true">{{ 'label.name' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.name" nz-input required name="name" type="text" id="name" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="value">{{ 'label.value' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.tagValue" nz-input name="value" type="text" id="value" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="color">{{ 'label.color' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input
            [(colorPicker)]="tag.color"
            cpFallbackColor="#64d3ca"
            cpOutputFormat="hex"
            [style.background]="tag.color"
            required
            style="width: 100%"
            class="br-4"
            name="color"
            id="color"
          />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="description">{{ 'label.description' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.description" nz-input name="description" type="text" id="description" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="tag.name != undefined">
        <nz-form-label [nzSpan]="7" nzFor="color">{{ 'label.display' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <nz-tag *ngIf="tag.tagValue == undefined || tag.tagValue.trim() == ''" [nzColor]="tag.color">{{ tag.name }}</nz-tag>
          <nz-tag *ngIf="tag.tagValue != undefined && tag.tagValue.trim() != ''" [nzColor]="tag.color">{{
            tag.name + ':' + tag.tagValue
          }}</nz-tag>
        </nz-form-control>
      </nz-form-item>
    </form>
  </div>
</nz-modal>
